﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.Screens;
using System.Collections;
using XWord.Fundamental.Domain;
using System.Reflection;
using XWord.Fundamental.Data.Repositories;
using XWord.Fundamental.Data.Impl;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
using System.IO;
namespace XWord.Fundamental
{
    public class WordPresenter
    {
        private readonly IWordRepository _wordRepository;
        private readonly ICategoryRepository _categoryRepository;
        private readonly EtymaRepository _etymaRepository;

        public WordPresenter()
        {
            return;
            _wordRepository = new WordRepository();
            _categoryRepository = new CategoryRepository();
            _etymaRepository = new EtymaRepository();
            WordList = new ObservableCollection<Word>(_wordRepository.LoadWordList());
            CategoryList = new ObservableCollection<Category>(_categoryRepository.GetAll());
            EtymaList = new ObservableCollection<Etymon>(_etymaRepository.GetEtymaList<Etymon>());
            SuffixList = new ObservableCollection<Suffix>(_etymaRepository.GetEtymaList<Suffix>());
            UnKnownWordCount = _wordRepository.GetUnKnownWordCount();
        }

        public IList<Word> WordList { get; set; }

        public IList<Category> CategoryList { get; set; }

        public IList<Etymon> EtymaList { get; set; }

        public IList<Suffix> SuffixList { get; set; }

        public int UnKnownWordCount { get; set; }

        public void MarkIsIgnore(IList selectedWord)
        {
            UnitOfWork.CurrentSession.Clear();
            MarkProperty(selectedWord, "IsIgnore");
        }

        public void IgnoreWord(IList selectedWord)
        {

            foreach (var item in selectedWord)
            {
                var word = item as Word;

                word.IsIgnore = true;
                _wordRepository.SaveWord(word);
            }
        }

        public void MarkIsIgnore(Word selectedWord)
        {
            UnitOfWork.CurrentSession.Clear();
            var list = new List<Word>();
            list.Add(selectedWord);
            MarkProperty(list, "IsIgnore");
        }

        public void MarkIsShortRead(IList selectedWord)
        {
            MarkProperty(selectedWord, "IsShortRead");
        }

        public void MarkIsExpert(IList selectedWord)
        {
            MarkProperty(selectedWord, "IsExpert");
        }

        public void MarkIsFamiliar(IList selectedWord)
        {
            MarkProperty(selectedWord, "IsFamiliar");
        }

        private void MarkProperty(IList selectedWord, string propertyName)
        {
            foreach (object item in selectedWord)
            {
                var currentProperty = item.GetType().GetProperty(propertyName);
                var currentVal = Convert.ToBoolean(currentProperty.GetValue(item, null));
                currentProperty.SetValue(item, !currentVal, null);
                _wordRepository.SaveWord(item as Word);
            }
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(WordList);
            collectionView.Refresh();
            //  MessageBox.Show("成功");
        }

        public void SaveWordSuffixComment(IList selectedWord)
        {

        }

        public void ReflashWordList()
        {
            WordList.Clear();
            foreach (var item in _wordRepository.GetAll())
            {
                WordList.Add(item);
            }
            //  WordList = new ObservableCollection<Word>(_wordRepository.GetAll());
        }

        public void SimpleWordSearch(string text)
        {
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(WordList);
            collectionView.Filter = new Predicate<object>((obj) =>
            {
                var entity = obj as Word;
                return entity.Name.StartsWith(text);
            });
        }

        public void ShortReadWordSearch()
        {
        }


        public void FilterCategoryWord(Category category)
        {
            var query = category.Words;

            ICollectionView collectionView = CollectionViewSource.GetDefaultView(WordList);
            collectionView.Filter = new Predicate<object>((obj) =>
            {
                var entity = obj as Word;
                return query.Contains(entity);
            });
        }

        public void FilterSuffixWord(Suffix suffix)
        {
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(WordList);
            collectionView.Filter = new Predicate<object>((obj) =>
            {
                var entity = obj as Word;
                return entity.Name.EndsWith(suffix.Name);
            });
        }

        public void ImportWordList()
        {
            UnitOfWork.CurrentSession.Clear();
            string line = string.Empty;
            using (TextReader streamReader =
                new StreamReader("D://develop//wpf//WordAssistant//WordAssistantApp//word3.txt", System.Text.Encoding.GetEncoding("GB2312")))
            {
                while ((line = streamReader.ReadLine()) != null)
                {
                    int ignore = line.IndexOf(" ");
                    if (ignore > 0)
                    {
                        string name = line.ToLower().Substring(0, ignore).ToLower().Trim();
                        if (WordList.Any(ee => ee.Name.Trim() == name))
                        {
                            Word word = WordList.First(ee => ee.Name.Trim() == name);
                            word.Comment = line.ToLower().Substring(ignore).Trim();
                        }
                        else
                        {
                            Word newword = new Word();
                            newword.Name = name;
                            newword.Suffix = string.Empty;
                            newword.Comment = line.ToLower().Substring(ignore).Trim();
                            WordList.Add(newword);
                            _wordRepository.SaveWord(newword);
                        }
                    }
                }
            }
        }

        public void ImportEtymonList()
        {
            UnitOfWork.CurrentSession.Clear();
            string line = string.Empty;


            using (TextReader streamReader =
                new StreamReader("D://develop//wpf//WordAssistant//WordAssistantApp//word4.txt", System.Text.Encoding.GetEncoding("GB2312")))
            {
                while ((line = streamReader.ReadLine()) != null)
                {
                    int ignore = line.IndexOf(" ");
                    if (ignore > 0)
                    {
                        string name = line.ToLower().Substring(0, ignore).ToLower().Trim();

                        if (WordList.Any(ee => ee.Name.Trim() == name))
                        {
                            Word word = WordList.First(ee => ee.Name.Trim() == name);
                            word.Suffix = line.ToLower().Substring(ignore).Trim();
                            _wordRepository.SaveWord(word);
                        }
                    }
                }
            }
        }



    }
}
